import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI} from '@/api'
const getDefaultState = () => { 
  return {
    token:getToken()//用户Token,
  }
}
const state=getDefaultState()

const mutations = {
  RESET_STATE: (state) => { 
    Object.assign(state,getDefaultState())
  },
  // 设置token
  SET_TOKEN(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  // 删除token
  REMOVE_TOKEN(state) {
    state.token = ''
    removeToken()
  }
}

const actions = {
  async loginActions({ commit }, value) { 
    //登录逻辑-封装
  const res = await loginAPI(value)
  //我们只需要token,保存到上面的vuex中
    commit('SET_TOKEN', res.data)
    return res
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}